package Base;

// 2、无头双向链表实现
public class LinkedList {

    //内部类构造一个链表数据结构
    static class ListNode{
        public int val;
        public ListNode prev;
        public ListNode next;
        public ListNode(){}
        public ListNode(int val){
            this.val=val;
        }
    }

    private ListNode first;
    private ListNode last;
    private int size=0;

    LinkedList(){}
    //头插法
    public void addFirst(int data){
        ListNode node=new ListNode(data);
        if ( first==null ){
            first=node;
            last=node;
        }else{
            node.next=first;
            first.prev=node;
            first=node;
        }
        size++;
    }

    //尾插法
    public void addLast(int data){
        ListNode node=new ListNode(data);
        if ( first==null ){
            first=node;
            last=node;
        }else{
            last.next=node;
            node.prev=last;
            last=node;
        }
        size++;
    }

    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data){
        //判断这个index是否合法
        if ( index<0 || index>size ){
            return false;
        }
        ListNode node=new ListNode(data);
        ListNode cur=first;
        for ( int i=0;i<index;i++ ){
            cur=cur.next;
        }
        if ( cur==first ){
            node.next=cur;
            cur.prev=node;
            first=node;
        }else if ( cur==last ){
            last.next=node;
            node.prev=last;
            last=node;
        }else{
            node.next=cur;
            node.prev=cur.prev;
            cur.prev.next=node;
            cur.prev=node;
        }
        return true;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur=first;
        while ( cur!=null ){
            if ( cur.val==key ){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key){
        ListNode cur=first;
        while ( cur!=null ) {
            if (cur.val == key) {
                //判断是不是头或尾
                if (cur == first) {
                    first=first.next;
                    if ( first!=null ){
                        first.prev=null;
                    }
                } else if (cur == last) {
                    last=last.prev;
                    last.next=null;
                } else {
                    cur.prev.next=cur.next;
                    cur.next.prev=cur.prev;
                }
                return;
            }
            cur = cur.next;
        }
    }

    //删除所有值为key的节点
    public void removeAllKey(int key){
        ListNode cur=first;
        while ( cur!=null ) {
            if (cur.val == key) {
                //判断是不是头或尾
                if (cur == first) {
                    first=first.next;
                    if ( first!=null ){
                        first.prev=null;
                    }
                } else if (cur == last) {
                    last=last.prev;
                    last.next=null;
                } else {
                    cur.prev.next=cur.next;
                    cur.next.prev=cur.prev;
                }
            }
            cur = cur.next;
        }
    }
    //得到单链表的长度
    public int size(){
        return this.size;
    }

    //输出链表的内容
    public void display(){
        ListNode cur=first;
        while ( cur != null ){
            System.out.println(cur.val);
            cur=cur.next;
        }
    }

    public void clear(){
        first=null;
        last=null;
    }
}
